<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends REST_Controller
{

    protected $rules = array(
        "send_sms" => array(
            array(
                'field' => 'Phone',
                'label' => '手机号码',
                'rules' => 'trim|required|is_mobile'
            ),
            array(
                'field' => 'Type',
                'label' => '请求类型',
                'rules' => 'trim|required|numeric'
            ),
        ),
        'check_sms'=>array(
            array(
                'field' => 'Phone',
                'label' => '手机号码',
                'rules' => 'trim|required|is_mobile'
            ),
            array(
                'field' => 'Type',
                'label' => '请求类型',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field'=>"Sms",
                'label' => '手机验证码',
                'rules' => 'trim|required'
            ),
        ),
        'signup'=>array(
            array(
                'field' => 'Uname',
                'label' => '姓名',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Phone',
                'label' => '手机号码',
                'rules' => 'trim|required|is_mobile|callback_mobile_exist'
            ),
            array(
                'field'   => 'Password',
                'label'   => '密码',
                'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
            ),
            array(
                'field'   => 'SmsCode',
                'label'   => '手机验证码',
                'rules'   => 'trim|required'
            ),
        ),
        "signin" => array(
            array(
                'field'   => 'Phone',
                'label'   => '手机号码',
                'rules'   => 'trim|required|callback_account_check'
            ),
            array(
                'field'   => 'Password',
                'label'   => '密码',
                'rules'   => 'trim|required|min_length[6]|max_length[20]|callback_pwd_check'
            ),
            array(//
                'field'   => 'Equipment',
                'label'   => '设备号',
                'rules'   => 'trim|'
            ),
        ),
        'find_password'=>array(
            array(
                'field'   => 'Phone',
                'label'   => '手机号码',
                'rules'   => 'trim|required|is_mobile|callback_getPhoneInfo_exist'
            ),
            array(
                'field'   => 'Password',
                'label'   => '密码',
                'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
            ),
            array(
                'field'   => 'SmsCode',
                'label'   => '手机验证码',
                'rules'   => 'trim|required'
            ),
        ),
        'reset_password'=>array(
            array(
                'field'   => 'Password',
                'label'   => '密码',
                'rules'   => 'trim|required|min_length[6]|max_length[20]'
            ),
            array(
                'field'   => 'NewPassword',
                'label'   => '密码',
                'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
            ),
        ),
        'info'=>array(
            array(
                'field'   => 'Uname',
                'label'   => '姓名',
                'rules'   => 'trim'
            ),
            array(
                'field'   => 'Position',
                'label'   => '职位',
                'rules'   => 'trim'
            ),
            array(
                'field'   => 'Sex',
                'label'   => '性别',
                'rules'   => 'trim|numeric'
            ),
            array(
                'field'   => 'Email',
                'label'   => '邮箱',
                'rules'   => 'trim|is_mail'
            ),
        ),
    );

    private $rdata = array();

    function __construct()
    {

        parent::__construct();
        $this->returnInfo = lang('returnInfo');
        $this->rdata['returnNo'] = '400';
        $this->rdata['returnInfo'] = '请求操作失败';
        $this->rdata['returnTime'] = time();
        $this->rdata['Secure'] = JSON_SECURE;
        $this->rdata['Content2'] = null;
        $this->rdata['Content'] = null;
        $this->rdata['returnData'] = null;
        $this->load->helpers('uisite_helper');
    }

    /**
     * 发送短信
     * phone
     * type请求类型 1注册，2忘记密码，3原手机号码，4新手机号码
     */
    public function send_sms_post(){
        session_start();
        $data= $this->post();
        $this->load->model('user_model','user','default');
        $this->form_validation->set_rules($this->rules['send_sms']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        if(!$this->checkPhone($data['Phone'])){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请输入正确的手机号码';
            $this->response($this->rdata);
        }
        if(!in_array($data['Type'],array(1,2,3,4))){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请求类型错误';
            $this->response($this->rdata);
        }
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $UID=$this->checkUserToken($token,$UID);
        $res=$this->user->setSendSms($data['Phone'],$data['Type'],$UID);
        $this->rdata['ReturnCode'] = $res['code'];
        $this->rdata['ReturnInfo'] = $res['msg'];
        $this->response($this->rdata);
    }

    /*
     * 校验验证码
     * phone
     * type请求类型 1注册，2忘记密码，3原手机号码，4新手机号码
     */
    public function check_sms_post(){
        session_start();
        $data= $this->post();
        $this->load->model('user_model','user','default');
        $this->form_validation->set_rules($this->rules['check_sms']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        if(!$this->checkPhone($data['Phone'])){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请输入正确的手机号码';
            $this->response($this->rdata);
        }
        if(!in_array($data['Type'],array(1,2,3,4))){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请求类型错误';
            $this->response($this->rdata);
        }
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $UID=$this->checkUserToken($token,$UID);
        $res=$this->user->check_sms($data['Phone'],$data['Type'],$UID,$data['Sms']);
        $this->rdata['ReturnCode'] = $res['code'];
        $this->rdata['ReturnInfo'] = $res['msg'];
        $this->response($this->rdata);
    }
    /*
     * 注册
     * Phone手机号码
     * Uname姓名
     * Password密码
     *SmsCode手机验证码
     */
    public function signup_post(){
        $data= $this->post();
        $this->load->model('user_model','user','default');
        $this->load->model('common_model','common','default');
        $this->form_validation->set_rules($this->rules['signup']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $info=$this->info;
        if(now()-$info['phone_code_time']>1800){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '验证码已过期，请重新发送';
            $this->response($this->rdata);
        }
        if($data['SmsCode']!=$info['phone_code']){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '手机验证码错误';
            $this->response($this->rdata);
        }
        $orderId='';
        while (1){
            $orderId=$this->orderId();
            $where=array('share_id'=>$orderId);
            if(!$this->user->countLog($where)){
                break;
            }
        }
        $update=array(
            'password'=>passwd($data['Password']),
            'phone_valid'=>1,
            'type'=>1,
            'uname'=>$data['Uname'],
            'share_id'=>$orderId,
            'timeline'=>now()
        );
        $res=$this->user->updateInfo($update,array('phone'=>$data['Phone'],'phone_valid'=>0));
        if($res){
            //$this->db->update('note',array('use'=>1),array('code'=>$data['SmsCode'],'phone'=>$data['Phone'],'type'=>1));
            $message=$this->user->onceInfo(array('phone'=>$data['Phone'],'phone_valid'=>1),'id,phone,uname,sex,position,email,type,share_id');
            $company=$this->common->onceInfo(array('user_id'=>$message['id']),'company_name,photo,status,vr_status,vr_url','user_company');
            if(empty($company)){
                $message['company_name']="";
                $message['logo']="";
                $message['status']=0;
                $message['vr_status']=0;
            }else{
                $message['company_name']=$company['company_name'];
                if(empty($company['photo'])){
                    $message['logo']="";
                }else{
                    $message['logo']=upload_file(tag_photo($company['photo']));
                }
                $message['status']=$company['status'];
                $message['vr_status']=$company['vr_status'];
            }
            if ($this->IS_APP) {
                $token = array('id' => $message['id'], 'logintime' => time());
                $message['token'] = $this->aes->encrypt(json_encode($token));
            }
            unset($message['id']);
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '注册成功';
            $this->rdata['Content2'] = $message;
            $this->rdata['Content'] = $message;
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '注册失败';
            $this->response($this->rdata);
        }
    }
    /*
     *登录
     */
    public function signin_post(){
        $data= $this->post();
        $this->load->model('user_model','user','default');
        $this->load->model('common_model','common','default');
        $this->form_validation->set_rules($this->rules['signin']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $info=$this->info;
        if ($this->IS_APP) {
            $token = array('id' => $info['id'], 'logintime' => time());
            $info['token'] = $this->aes->encrypt(json_encode($token));
        }
        $Equipment=isset($data['Equipment'])?$data['Equipment']:'';
        $ip=$this->input->ip_address();
        $update=array(
            'login_ip'=>$ip,
            'login_time'=>now(),
            'login_ip_prev'=>$info['login_ip'],
            'login_time_prev'=>$info['login_time'],
            'equipment'=>$Equipment,
            'equipment_prev'=>$info['equipment']
        );
        $this->user->updateInfo($update,array('id'=>$info['id']));
        $message=array();
        $message['id']=$info['id'];
        $message['phone']=$info['phone'];
        $message['uname']=$info['uname'];
        $message['sex']=$info['sex'];
        $message['position']=$info['position'];
        $message['email']=$info['email'];
        $message['type']=$info['type'];
        $message['share_id']=$info['share_id'];
        //获取公司信息
        $company=$this->common->onceInfo(array('user_id'=>$info['id']),'company_name,photo,status,vr_status,vr_url','user_company');
        if(empty($company)){
            $message['company_name']="";
            $message['logo']="";
            $message['status']=0;
            $message['vr_status']=0;
        }else{
            $message['company_name']=$company['company_name'];
            if(empty($company['photo'])){
                $message['logo']="";
            }else{
                $message['logo']=upload_file(tag_photo($company['photo']));
            }
            if($company['status']=""){
                $message['status']=0;
            }else{
                $message['status']=$company['status'];
            }
            $message['vr_status']=$company['vr_status'];
        }
        if ($this->IS_APP) {
            $token = array('id' => $message['id'], 'logintime' => time());
            $message['token'] = $this->aes->encrypt(json_encode($token));
        }
        unset($message['id']);
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '登录成功';
        $this->rdata['Content2'] = $message;
        $this->rdata['Content'] = $message;
        $this->response($this->rdata);
    }

    /*
     * 找回密码
     */
    public function find_password_patch(){
        $_POST=$this->patch();
        $data=$_POST;
        $this->load->model('user_model','user','default');
        $this->form_validation->set_rules($this->rules['find_password']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $info=$this->info;
        if(empty($info['find_pwd_code_time'])){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请发送验证码';
            $this->response($this->rdata);
        }
        if(now()-$info['find_pwd_code_time']>1800){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '未发送验证码或者验证码已过期，请重新发送';
            $this->response($this->rdata);
        }
        if($data['SmsCode']!=$info['find_pwd_code']){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '手机验证码错误';
            $this->response($this->rdata);
        }
        $data=array(
            'password'=>passwd($data['Password']),
        );
        $this->user->updateInfo($data,array('id'=>$info['id']));
        //$this->db->update('note',array('use'=>1),array('code'=>$data['SmsCode'],'phone'=>$data['Phone'],'type'=>2));
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '密码已找回';
        $this->response($this->rdata);
    }
    /*
     * 修改密码
     */
    public function  reset_password_patch(){
        session_start();
        $_POST= $this->patch();
        $data=$_POST;
        $this->load->model('user_model','user','default');
        $this->form_validation->set_rules($this->rules['reset_password']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $UID=$this->checkToken($token,$UID);
        if(empty($UID)){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '登录已失效';
            $this->response($this->rdata);
        }
        $res=$this->user->onceInfo(array('id'=>$UID),'password');
        if(passwd($data['Password'])!=$res['password']){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '原密码错误';
            $this->response($this->rdata);
        }
        $data=array(
            'password'=>passwd($data['NewPassword']),
        );
        $v=$this->user->updateInfo($data,array('id'=>$UID));
        if($v){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '修改密码成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '修改密码失败';
            $this->response($this->rdata);
        }
    }

    /**
     * 获取账户信息
     */
    public function info_get(){
        session_start();
        $this->load->model('user_model','user','default');
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $UID=$this->checkToken($token,$UID);
        $count=$this->user->countLog(array('id'=>$UID,'phone_valid'=>1,'is_delect'=>0));
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该用户不存在';
            $this->response($this->rdata);
        }
        $res=$this->user->onceInfo(array('id'=>$UID,'phone_valid'=>1),'uname,position,type,sex,phone,email');
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '账户信息获取成功';
        $this->rdata['Content2'] = $res;
        $this->rdata['Content'] = $res;
        $this->response($this->rdata);
    }
    /**
     * 修改账户信息
     */
    public function info_patch(){
        session_start();
        $_POST=$this->patch();
        $data=$_POST;
        $this->load->model('user_model','user','default');
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $UID=$this->checkToken($token,$UID);
        $this->form_validation->set_rules($this->rules['info']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $count=$this->user->countLog(array('id'=>$UID,'phone_valid'=>1,'is_delect'=>0));
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该用户不存在';
            $this->response($this->rdata);
        }

        $res=$this->user->onceInfo(array('id'=>$UID,'phone_valid'=>1),'uname,position,type,sex,phone,email');
        $uname=isset($data['Uname'])?$data['Uname']:$res['uname'];
        $sex=isset($data['Sex'])?$data['Sex']:$res['sex'];
        $position=isset($data['Position'])?$data['Position']:$res['position'];
        if(!empty($data['Email'])){
            $email_count=$this->user->countLog(array('email'=>$data['Email'],'is_delect'=>0));
            if($email_count>0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '该邮箱已使用';
                $this->response($this->rdata);
            }
        }
        $email=isset($data['Email'])?$data['Email']:$res['email'];
        $update=array(
            'uname'=>$uname,
            'position'=>$position,
            'sex'=>$sex,
            'email'=>$email
        );
        $v=$this->user->updateInfo($update,array('id'=>$UID,'phone_valid'=>1));
        if($v){
            $message=$this->user->onceInfo(array('id'=>$UID,'phone_valid'=>1),'uname,position,type,sex,phone,email');
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '修改成功';
            $this->rdata['Content2'] = $message;
            $this->rdata['Content'] = $message;
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '修改失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 获取消息中心的系统消息和推广消息的详情信息
     */
    public function getMessageInfo_get(){
        session_start();
        $this->load->model('common_model','common','default');
        $this->load->model('user_model','user','default');
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $data=$this->get();
        $UID=$this->checkUserToken($token,$UID);
        if($UID){
            $count=$this->user->countLog(array('id'=>$UID,'phone_valid'=>1,'is_delect'=>0));
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '该用户不存在';
                $this->response($this->rdata);
            }
            $userid=$UID;
        }else{
            $share_id=isset($data['ShareId'])?$data['ShareId']:'';

            if(!$share_id){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '您的登录已经失效，请重新登录';
                $this->response($this->rdata);
            }else{
                $res=$this->user->onceInfo(array('share_id'=>$share_id,'phone_valid'=>1),'id');
                if($res){
                    $userid=$res['id'];
                }else{
                    $this->rdata['ReturnCode']   = '400';
                    $this->rdata['ReturnInfo'] = '您的登录已经失效，请重新登录';
                    $this->response($this->rdata);
                }
            }
        }
        if($data['Type']==1){
            $count=$this->common->countLog(array('cid'=>6,'audit'=>1,'id'=>$data['Id']),'message');
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '内容不存在';
                $this->response($this->rdata);
            }
            $res=$this->common->onceInfo(array('cid'=>6,'audit'=>1,'id'=>$data['Id']),'id,title,content,timeline','message');
            $res['timeline']=date('Y-m-d H:i:s',$res['timeline']);
            $vdata['info']=$res;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }elseif($data['Type']==2){
            $count=$this->common->countLog(array('cid'=>7,'audit'=>1,'id'=>$data['Id']),'message');
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '内容不存在';
                $this->response($this->rdata);
            }
            $res=$this->common->onceInfo(array('cid'=>7,'audit'=>1,'id'=>$data['Id']),'id,title,content,timeline','message');
            $res['timeline']=date('Y-m-d H:i:s',$res['timeline']);
            $vdata['info']=$res;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请求类型错误';
            $this->response($this->rdata);
        }
    }
    //返回省
    public function province_get(){
        $this->load->model('common_model','common','default');
        $list=$this->db->select('id,fullname')
            ->where(array('level'=>1))
            ->get('system_districts')
            ->result_array();
        $this->rdata['ReturnCode']='200';
        $this->rdata['ReturnInfo']='获取成功';
        $this->rdata['Content']['list']=$list;
        $this->response($this->rdata);
    }

    //返回市
    public function city_get(){
        $this->load->model('common_model','common','default');
        $Fid=intval($this->get('Id'));
        $where=array('level'=>2,'parentid'=>$Fid);
        $list=$this->db->select('id,fullname')
            ->where($where)
            ->get('system_districts')
            ->result_array();
        if(count($list)){
            $this->rdata['ReturnCode']='200';
            $this->rdata['ReturnInfo']='获取成功';
            $this->rdata['Content']['list']=$list;
        }
        $this->response($this->rdata);
    }

    //返回区
    public function area_get(){
        $this->load->model('common_model','common','default');
        $Fid=intval($this->get('Id'));
        $where=array('level'=>3,'parentid'=>$Fid);
        $list=$this->db->select('id,fullname')
            ->where($where)
            ->get('system_districts')
            ->result_array();
        $this->rdata['ReturnCode']='200';
        $this->rdata['ReturnInfo']='获取成功';
        $this->rdata['Content']['list']=$list;

        $this->response($this->rdata);
    }
    //返回省市区JSON文件
    public function addressJson_get(){
        $this->load->model('user_model','user','default');
        $allData=$this->db->select('id,parentid,fullname,level')
            ->where(array('in'=>array('level',array(1,2,3))))->get('system_districts')
            ->result_array();
        $province=array();
        foreach ($allData as $k=>$val){
            if($val['level']==1){
                $province[]=array(
                    'id'=>$val['id'],
                    'fullname'=>$val['fullname']
                );
                unset($allData[$k]);
            }
        }
        foreach ($province as $k=>$item){
            foreach ($allData as $k2=>$val){
                if($val['parentid']==$item['id']){
                    $province[$k]['city'][]=array(
                        'id'=>$val['id'],
                        'fullname'=>$val['fullname']
                    );
                    unset($allData[$k2]);
                }
            }
        }
        foreach ($province as $k=>$item){
            foreach ($item['city'] as $k2=>$item2){
                foreach ($allData as $k3=>$item3){
                    if($item3['parentid']==$item2['id']){
                        $province[$k]['city'][$k2]['area'][]=array(
                            'id'=>$item3['id'],
                            'fullname'=>$item3['fullname']
                        );
                        unset($allData[$k3]);
                    }

                }
            }
        }
        echo json_encode($province,JSON_UNESCAPED_UNICODE);
    }

    /*
    * 退出
    */
    public function signout_post() {
        session_start();
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $UID=$this->checkToken($token,$UID);
        $this->session->sess_destroy();
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] ='退出成功';
        $this->response($this->rdata);
    }
    public function getPhoneInfo_exist($phone=false){
        if(!$this->checkPhone($phone)){
            $this->form_validation->set_message('getPhoneInfo_exist', '手机号码格式不正确');
            return FALSE;
        }
        $count=$this->user->countLog(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0));
        if($count==0){
            $this->form_validation->set_message('getPhoneInfo_exist', '该手机号未注册');
            return FALSE;
        }
        $this->info=$this->user->onceInfo(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0),'id,phone,find_pwd_code,find_pwd_code_time,phone_code,phone_code_time');
        return TRUE;
    }
    public function mobile_exist($phone=false){
        $count=$this->user->countLog(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0));
        if($count>0){
            $this->form_validation->set_message('mobile_exist', '该手机号已注册');
            return FALSE;
        }
        $count=$this->user->countLog(array('phone'=>$phone,'phone_valid'=>0));
        if($count==0){
            $this->form_validation->set_message('mobile_exist', '请先获取手机验证码');
            return FALSE;
        }
        $this->info=$this->user->onceInfo(array('phone'=>$phone,'phone_valid'=>0),'phone_code,phone_code_time');
        return TRUE;
    }
    public function account_check($phone = false){
        $count=$this->user->countLog(array('email'=>$phone,'phone_valid'=>1,'is_delect'=>0));
        if($count==0){
            $count1=$this->user->countLog(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0));
            if($count1==0){
                $this->form_validation->set_message('account_check', '用户名或密码错误,登录失败');
                return FALSE;
            }else{
                $this->info=$this->user->onceInfo(array('phone'=>$phone,'phone_valid'=>1,'is_delect'=>0),'*');
            }
        }else{
            $this->info=$this->user->onceInfo(array('email'=>$phone,'phone_valid'=>1,'is_delect'=>0),'*');
        }
        return TRUE;
    }
    private  function checkUserToken($token,$UID){
        if (! $token && ! $UID) {
            return false;
        }
        if ($token_info = json_decode($this->aes->decrypt($token,TokenKEY), 1)) {
            $UID = isset($token_info['id']) ? $token_info['id'] : 0;
        }
        if(!$token_info){
            return false;
        }
        return $UID;
    }
    private  function checkToken($token,$UID){
        if (! $token && ! $UID) {
            $this->rdata['ReturnCode']   = '0014';
            $this->rdata['ReturnInfo'] = '您的登录已经失效，请重新登录';
            $this->response($this->rdata);
        }
        if ($token_info = json_decode($this->aes->decrypt($token,TokenKEY), 1)) {
            $UID = isset($token_info['id']) ? $token_info['id'] : 0;
        }
        if(!$token_info){
            $this->rdata['ReturnCode']   = '0011';
            $this->rdata['ReturnInfo'] = 'TOKEN 验证失败';
            $this->response($this->rdata);
        }
        return $UID;
    }
    public function testSend_get(){
        $content='<table style="padding-left: 35%"><tr><td>用户名:</td><td>唐舞桐</td></tr><tr><td>手机号码:</td><td>15926290378</td></tr><tr><td>反馈内容:</td><td>c测试</td></tr></table>';
        $me=$this->send('1181452567@qq.com','海南智造慧科技有限公司','意见反馈 ',$content);
    }
    public function send($to, $username, $title, $content)
    {
        $this->load->model('user_model','user','default');
        $this->load->library('mailer');
        //加载发送SMTP服务器配置
        $config = $this->db->where(array('category' => 'email'))->select('value')->order_by('id asc')->get('configs')->result_array();
        $obj = new Mailer();
        $obj->mail->Host = $config[0]['value'];
        $obj->mail->Port = $config[3]['value'];
        $obj->mail->Username = $config[1]['value'];
        $obj->mail->Password = $config[2]['value'];
        $title = $config[4]['value'];
        $res = $obj->sendmail(
            $to,
            $username,
            $title,
            $content
        );
        return $res;
    }
    // 验证密码是否正确
    public function pwd_check($password = false)
    {

        // 消除通过路由的请求
        if($this->router->method == 'pwd_check'){
            show_404();
        }
        // 帐号存在则过
        if (isset($this->info) and $this->info and is_array($this->info) ) {
            if ($password and passwd($password) == $this->info['password']) {
                return TRUE;
            }
            $array=array(
                'pwd_errors'=>$this->info['pwd_errors']+1
            );
            $this->user->updateInfo($array,array('id'=>$this->info['id']));
            $this->form_validation->set_message('pwd_check', '用户名或密码错误,登录失败');
        } else {

            $this->form_validation->set_message('pwd_check', '用户不存在');
        }
        return FALSE;

    }
    private function orderId(){
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T');
        $orderSn = $yCode[intval(date('Y')) - 2017] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }
    private function checkPhone($phone)
    {
        if (preg_match('/^1[2345678]\d{9}$/', $phone)) {
            return true;
        } else {
            return false;
        }
    }


    private function em_getallheaders($key=false) {

        foreach ($_SERVER as $name => $value) {

            if (substr($name, 0, 5) == 'HTTP_') {

                $headers[str_replace(' ', '-', ucwords(strtoupper(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        if ($key && isset($headers[$key])) {

            return $headers[$key];
        }
        return false;
    }
}